/*
 * PermissionTag.java
 *
 * Created on August 26, 2003, 3:33 PM
 */

package com.scalar.chiptrack.tags;

/**
 *
 * @author  Administrator
 * @version 
 */

import java.io.IOException;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;

import org.apache.struts.taglib.html.ErrorsTag;

import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;


/**
 * Custom tag that displays a message that user doesn't have any modify rights.
 */

public class PermissionTag extends ErrorsTag {

    private final static String FONT_COLOR = "blue";
    private boolean m_showMessage = false;

    /** Creates new PermissionTag */
    public PermissionTag () {
    }

    
    /**
     * class property
     */
    private String m_styleClass = "";
    
    public String getStyleClass() 
    {
        return m_styleClass;
    }
    
    public void setStyleClass (String styleClass) 
    {
        if (styleClass != null) {
            m_styleClass = styleClass;
        }
    }

    /**
     * Functioanlity attribute
     */
    private String m_functionality = null;
    
    public String getFunctionality() 
    {
        return m_functionality;
    }
    
    public void setFunctionality (String functionality) 
    {
        m_functionality = functionality;
    }
    
    /**
     * Action Property
     */
    private String m_action = User.USER_ACTION_VIEW;
    
    public String getAction() 
    {
        return m_action;
    }
    
    public void setAction (String action) 
    {
        if (action != null) 
        {
            m_action = action;
        }
    }
    
    private String m_userMessage = null;
    
    public String getUserMessage()
    {
        return m_userMessage;
    }

    public void setUserMessage(String userMessage)
    {
        if ( userMessage != null )
        {
            m_userMessage = userMessage;
        }
    }

    private boolean m_append = false;
    
    public boolean getAppend()
    {
        return m_append;
    }

    public void setAppend(boolean append)
    {
        m_append = append;
    }

    public int doStartTag() throws JspException 
    {

        // Dont show the link if its not allowed
        m_showMessage = showMessage();
        if (m_showMessage == true) 
        {
            return (EVAL_BODY_INCLUDE);
        }

        StringBuffer permission = new StringBuffer();
        permission.append ("<table border='0' bordercolor='#FFFFFF'>");

        permission.append ("<tr class='" + m_styleClass + "'>");
        permission.append ("<td align='left'><font color='" + FONT_COLOR +"'>");
        if ( m_userMessage == null )
        {
            permission.append ("<bold><li>" + Constants.NO_MODIFY_RIGHTS + "</bold>");
        }
        if ( ( m_userMessage != null ) && ( m_append == true ) )
        {
            permission.append ("<bold><li>" + Constants.NO_MODIFY_RIGHTS + "</bold>");
            permission.append ("<bold>&nbsp;&nbsp;" + m_userMessage + "</bold>");
        }
        if ( ( m_userMessage != null ) && ( m_append == false ) )
        {
            permission.append ("<bold><li>" + m_userMessage + "</bold>");
        }
        permission.append ("</font></td></tr></table>");
          
        try
        {
            //Print the results to our output writer
            pageContext.getOut().print(permission.toString());
        }
        catch (IOException ioe)
        {
            System.out.println ("Exception Message: " + ioe.getMessage());
        }
        
        catch  (Throwable e)
        {
            e.printStackTrace();
        }
        return (EVAL_BODY_INCLUDE);
    }
    
    
    
    private boolean showMessage() 
    {
        
        if (m_functionality == null) 
        {
            return false;
        }
        
        HttpSession session = pageContext.getSession();
        if (session == null) 
        {
            return false;
        }

        User user = (User) session.getAttribute (SessionKeys.USER_KEY);
        if (user == null) {
            return false;
        }
        
        return user.isActionAllowed (m_functionality, m_action);
    }
    
}
